import Toast from '../component/Toast';
import {contains} from './utils';

/**
 * plus执行函数
 * @param {Function} fn
 */
function execute(fn) {
	fn = fn || function() {};
	if(window.plus) {
		fn();
	} else {
		document.addEventListener('plusready', fn, false);
	}
}

/**
 * 初始化
 * @param {String} statusBarBackground
 * @param {String} statusBarStyle
 * @param {Function} backButtonListenerFn
 */
function init(statusBarBackground, statusBarStyle, backButtonListenerFn) {
	execute(function() {
		if(statusBarBackground) plus.navigator.setStatusBarBackground(statusBarBackground); // 系统状态栏默认颜色
		if(statusBarStyle) plus.navigator.setStatusBarStyle(statusBarStyle); // 设置系统状态栏样式
		backButtonListener(backButtonListenerFn);
	});
}

function initWeb(statusBarBackground, statusBarStyle, backFn) {
	let quit = false;
	init(statusBarBackground, statusBarStyle, function() {
		if(backFn()) {
			return false;
		} else {
			if(!quit) {
				quit = true;
				// html toast
				Toast.show('再按一次退出', 2000);
				setTimeout(function() {
					quit = false;
				}, 2000);
				return false;
			}
			return true;
		}
	});
}

// 单次回退模式
function initOneBack(statusBarBackground, statusBarStyle) {
	init(statusBarBackground, statusBarStyle, function() {
		back();
	});
}

// 双次退出模式
function initDoubleQuit(statusBarBackground, statusBarStyle) {
	let quit = false;
	init(statusBarBackground, statusBarStyle, function() {
		if(!quit) {
			quit = true;
			// 原生toast
			toast('再按一次退出', {
				duration: 'short'
			});
			setTimeout(function() {
				quit = false;
			}, 2000);
			return false;
		}
		return true;
	});
}

/**
 * 回退,通过重写overrideBack可以自定义回退方法
 */
function back() {
	let _back = function() {
		if(window.overrideBack) {
			window.overrideBack();
		} else {
			let displayWvs = plus.webview.getDisplayWebview();
			if(displayWvs) {
				for(let i = 0; i < displayWvs.length; i++) {
					invoke(displayWvs[i].id, 'close');
				}
			}
		}
	}
	execute(function() {
		if(window.preBack) preBack();
		_back();
		if(window.afterBack) afterBack();
	});
}

/**
 * 退出应用
 */
function quit() {
	execute(function() {
		plus.runtime.quit();
	});
}

/**
 * Android回退键监听
 * @param {Function} backbuttonFn
 */
function backButtonListener(backbuttonFn) {
	execute(function() {
		plus.key.addEventListener('backbutton', function() {
			if(backbuttonFn && backbuttonFn()) quit();
		}, false);
	});
}

function isAndroid() {
	return plus.os.name && plus.os.name.toLowerCase() == "android";
}

function isIos() {
	return plus.os.name && plus.os.name.toLowerCase() == "ios";
}

/**
 * 原生提示
 * @param {String} msg 信息内容
 * @param {Json} style
 */
function toast(msg, style) {
	execute(function() {
		plus.nativeUI.toast(msg, style);
	});
	return Webview;
}

/**
 * 沉浸式状态栏高度
 * @param {Object} callback
 */
function immersedStatusbarHeight(callback) {
	execute(() => {
		if(plus.navigator.isImmersedStatusbar() && callback) {
			callback(plus.navigator.getStatusbarHeight());
		}
	});
}

export {
	execute,
	init,
	initWeb,
	initOneBack,
	initDoubleQuit,
	back,
	quit,
	backButtonListener,
	isAndroid,
	isIos,
	toast,
	immersedStatusbarHeight,
}
